package com.sinheldrin.musicplayer.player
{
	import com.sinheldrin.musicplayer.music.Music;
	import com.sinheldrin.musicplayer.music.Playlist;
	
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.system.Security;
	
	import mx.containers.Canvas;
	import mx.controls.SWFLoader;
	import mx.core.Container;
	
	import spark.components.TextInput;
	
	public class YoutubePlayer extends Canvas
	{
		// Constants
		private static const DEFAULT_VIDEO_ID:String = "RsQEPPQa2Xo";
		private static const YOUTUBE_API_URL:String = "http://www.youtube.com/apiplayer?version=3";
		
		private static const STATE_ENDED:Number = 0;
		private static const STATE_PLAYING:Number = 1;
		private static const STATE_PAUSED:Number = 2;
		private static const STATE_CUED:Number = 5;
		
		
		private var player_:Object;
		private var playerLoader_:SWFLoader;
		private var youtubeApiLoader_:URLLoader;
		
		private var currentPlaylist_:Playlist = null;
		private var currentMusic_:Music = null;
		
		private var _isPlaying:Boolean = false;

		[Bindable]
		public function get isPlaying():Boolean
		{
			return _isPlaying;
		}

		private function set isPlaying(value:Boolean):void
		{
			_isPlaying = value;
		}
		
		private var _isRepeatEnabled:Boolean = false;

		[Bindable]
		public function get isRepeatEnabled():Boolean
		{
			return _isRepeatEnabled;
		}

		public function set isRepeatEnabled(value:Boolean):void
		{
			_isRepeatEnabled = value;
		}


		public function set volume(value:int):void
		{
			if (player_ != null)
				player_.setVolume(value);
		}
		
		[Bindable]
		public function get CurrentPlaylist():Playlist
		{
			return currentPlaylist_;
		}

		public function set CurrentPlaylist(value:Playlist):void
		{
			currentPlaylist_ = value;
		}
		
		
		public function YoutubePlayer ()
		{
			super();
			
			CurrentPlaylist = new Playlist ();
		}
		
		public function Init ():void
		{
			// Youtube API Comment
			/* The player SWF file on www.youtube.com needs to communicate with your host
			/* SWF file. Your code must call Security.allowDomain() to allow this
			/* communication. */
			// Actually throw a security exception and isn't necessary. 
			//Security.allowDomain ("www.youtube.com");
			
			InitLoader ();
			//InitYouTubeApiLoader ();
		}
		
		private function InitLoader():void
		{
			playerLoader_ = new SWFLoader ();
			playerLoader_.addEventListener (Event.INIT, LoaderInitHandler);
			playerLoader_.load (YOUTUBE_API_URL);
		}
		
		private function LoaderInitHandler (event:Event):void
		{
			addChild(playerLoader_);
			playerLoader_.content.addEventListener("onReady", onPlayerReady);
			playerLoader_.content.addEventListener("onError", onPlayerError);
			playerLoader_.content.addEventListener("onStateChange", onPlayerStateChange);
		}
		
		private function onPlayerReady(event:Event):void
		{
			player_ = playerLoader_.content;
			player_.setSize(200, 200);
			//player_.visible = false;
			
			//player_.loadVideoById (DEFAULT_VIDEO_ID);
		}
		
		private function onPlayerError(event:Event):void
		{
			trace("Player error:", Object(event).data);
		}
		
		private function onPlayerStateChange(event:Event):void
		{
			trace("State is", Object(event).data);
			
			switch (Object(event).data) {
				case STATE_ENDED:
					isPlaying = false;
					if (isRepeatEnabled == true)
						next();
					break;
				
				case STATE_PLAYING:
					break;
				
				case STATE_PAUSED:
					break;
				
				case STATE_CUED:
					break;
			}
		}
		
		/*private function InitYouTubeApiLoader():void
		{
			youtubeApiLoader_ = new URLLoader ();
			youtubeApiLoader_.addEventListener(IOErrorEvent.IO_ERROR, youtubeApiLoaderErrorHandler);
			youtubeApiLoader_.addEventListener(Event.COMPLETE, youtubeApiLoaderCompleteHandler);
		}
		
		private function youtubeApiLoaderErrorHandler(event:IOErrorEvent):void
		{
			trace ("Error making YouTube API request:", event);
		}
		
		private function youtubeApiLoaderCompleteHandler(event:Event):void
		{
			var atomData:String = youtubeApiLoader_.data;
			
			// Parse the YouTube API XML response and get the value of the
			// aspectRatio element.
			var atomXml:XML = new XML(atomData);
			var aspectRatios:XMLList = atomXml..*::aspectRatio;
			
			isWidescreen = aspectRatios.toString() == WIDESCREEN_ASPECT_RATIO;
			
			isQualityPopulated = false;
			
			player_.loadVideoById (DEFAULT_VIDEO_ID);
		}*/
		
		public function play ():void
		{
			if (isPlaying == false)
			{
				player_.playVideo();
				isPlaying = true;
			}
		}
		
		public function PlayMusic(music:Music):void
		{
			stop();
			currentMusic_ = music;
			player_.cueVideoById (music.VideoId);
			play ();
		}
		
		public function pause ():void
		{
			if (isPlaying == true)
			{
				player_.pauseVideo();
				isPlaying = false;
			}
		}
		
		public function stop():void
		{
			if (isPlaying == true)
			{
				player_.stopVideo();
				isPlaying = false;
			}
		}
		
		public function next():void
		{
			PlayMusic(CurrentPlaylist.nextMusic(currentMusic_));
		}
	}
}